package top.wardyan.wdev.dbdevassist.action.user;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.conf.Const;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WText;
//import top.wardyan.control.WText;
import top.wardyan.listener.WNumberOnlyKeyAdapter;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.action.base.BaseMasterEditShell;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTUser;
import top.wardyan.wdev.dbdevassist.service.IMasterService;

public class UserEditShell2 extends BaseMasterEditShell<UserEditShell2, YTUser>{
	
	protected WComposite mainCom;
	protected WText txtSID;
	protected WText txtXName;
	protected WText txtXAccount;
	protected WText txtXPassword;
	protected WText txtXIP;
	protected WText txtXPCName;
	protected WText txtXNote;
	
	public UserEditShell2() {
		this(11, DDA.getInstance().userService);
	}
	
	public UserEditShell2(int sID, IMasterService<YTUser> masterService) {
		super(sID, masterService);
		this.createContent();
		//this.initBtnStatus();
		
	}
	
	
	
	private void createContent() {
		mainCom = new WComposite(this, SWT.BORDER);
		mainCom.setLayout(new FormLayout());
		//mainCom.setLayout(layout);
		FormData fd_mainCom = new FormData();
		fd_mainCom.left = new FormAttachment(0);
		fd_mainCom.top = new FormAttachment(topCom, 5);
		fd_mainCom.right = new FormAttachment(100, -1);
		fd_mainCom.bottom = new FormAttachment(100, -1);
		mainCom.setLayoutData(fd_mainCom);
		
		WComposite comSID = new WComposite(mainCom, SWT.NONE);
		//comSID.setSize(300, 30);
		comSID.setLayout(new FormLayout());
		FormData fd_comSID = new FormData();
		fd_comSID.left = new FormAttachment(mainCom, 4);
		fd_comSID.top = new FormAttachment(mainCom, 0);
		fd_comSID.right = new FormAttachment(comSID, 300);
		fd_comSID.right = new FormAttachment(25, 0);
		fd_comSID.bottom = new FormAttachment(comSID, 35);
		comSID.setLayoutData(fd_comSID);
		
		WLabel lblSID = new WLabel(comSID, SWT.BORDER, Const.FONT16, "sID", SWT.RIGHT);
		FormData fd_lblSID = new FormData();
		fd_lblSID.left = new FormAttachment(comSID, 0);
		fd_lblSID.top = new FormAttachment(comSID, 2);
		fd_lblSID.right = new FormAttachment(30);
		fd_lblSID.bottom = new FormAttachment(lblSID, 30);
		lblSID.setLayoutData(fd_lblSID);
		txtSID = new WText(comSID, SWT.BORDER, "", "");
		txtSID.setTextLimit(9);
		txtSID.addKeyListener(new WNumberOnlyKeyAdapter());
		FormData fd_txtSID = new FormData();
		fd_txtSID.left = new FormAttachment(lblSID, 4);
		fd_txtSID.top = new FormAttachment(comSID, 2);
		fd_txtSID.right = new FormAttachment(100, -8);
		fd_txtSID.bottom = new FormAttachment(txtSID, 30);
		txtSID.setLayoutData(fd_txtSID);
		txtSID.setEditable(false);
		
		WComposite comXName = new WComposite(mainCom, SWT.NONE);
		comXName.setLayout(new FormLayout());
		FormData fd_comXName = new FormData();
		fd_comXName.left = new FormAttachment(comSID, 4);
		fd_comXName.top = new FormAttachment(mainCom, 0);
		fd_comXName.right = new FormAttachment(50, 0);
		fd_comXName.bottom = new FormAttachment(comXName, 35);
		comXName.setLayoutData(fd_comXName);
		
		WLabel lblXName = new WLabel(comXName, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXName = new FormData();
		fd_lblXName.left = new FormAttachment(comXName, 0);
		fd_lblXName.top = new FormAttachment(comXName, 2);
		fd_lblXName.right = new FormAttachment(30);
		fd_lblXName.bottom = new FormAttachment(lblXName, 30);
		lblXName.setLayoutData(fd_lblXName);
		txtXName = new WText(comXName, SWT.BORDER, "", "名称");
		txtXName.setTextLimit(32);
		FormData fd_txtXName = new FormData();
		fd_txtXName.left = new FormAttachment(lblXName, 4);
		fd_txtXName.top = new FormAttachment(comXName, 2);
		fd_txtXName.right = new FormAttachment(100, -8);
		fd_txtXName.bottom = new FormAttachment(txtXName, 30);
		txtXName.setLayoutData(fd_txtXName);
		
		// 账号
		WComposite comXAccount = new WComposite(mainCom, SWT.NONE);
		comXAccount.setLayout(new FormLayout());
		FormData fd_comXAccount = new FormData();
		fd_comXAccount.left = new FormAttachment(comXName, 4);
		fd_comXAccount.top = new FormAttachment(mainCom, 0);
		fd_comXAccount.right = new FormAttachment(75, 0);
		fd_comXAccount.bottom = new FormAttachment(comXAccount, 35);
		comXAccount.setLayoutData(fd_comXAccount);
		WLabel lblXAccount = new WLabel(comXAccount, SWT.BORDER, Const.FONT16, "账号", SWT.RIGHT);
		FormData fd_lblXAccount = new FormData();
		fd_lblXAccount.left = new FormAttachment(comXAccount, 0);
		fd_lblXAccount.top = new FormAttachment(comXAccount, 2);
		fd_lblXAccount.right = new FormAttachment(30);
		fd_lblXAccount.bottom = new FormAttachment(lblXAccount, 30);
		lblXAccount.setLayoutData(fd_lblXAccount);
		txtXAccount = new WText(comXAccount, SWT.BORDER, "", "账号");
		txtXAccount.setTextLimit(32);
		FormData fd_txtXAccount = new FormData();
		fd_txtXAccount.left = new FormAttachment(lblXAccount, 4);
		fd_txtXAccount.top = new FormAttachment(comXAccount, 2);
		fd_txtXAccount.right = new FormAttachment(100, -8);
		fd_txtXAccount.bottom = new FormAttachment(txtXAccount, 30);
		txtXAccount.setLayoutData(fd_txtXAccount);
		
		// 密码
		WComposite comXPassword = new WComposite(mainCom, SWT.NONE);
		comXPassword.setLayout(new FormLayout());
		FormData fd_comXPassword = new FormData();
		fd_comXPassword.left = new FormAttachment(comXAccount, 4);
		fd_comXPassword.top = new FormAttachment(mainCom, 0);
		fd_comXPassword.right = new FormAttachment(100, 0);
		fd_comXPassword.bottom = new FormAttachment(comXPassword, 35);
		comXPassword.setLayoutData(fd_comXPassword);
		WLabel lblXPassword = new WLabel(comXPassword, SWT.BORDER, Const.FONT16, "密码", SWT.RIGHT);
		FormData fd_lblXPassword = new FormData();
		fd_lblXPassword.left = new FormAttachment(comXPassword, 0);
		fd_lblXPassword.top = new FormAttachment(comXPassword, 2);
		fd_lblXPassword.right = new FormAttachment(30);
		fd_lblXPassword.bottom = new FormAttachment(lblXPassword, 30);
		lblXPassword.setLayoutData(fd_lblXPassword);
		txtXPassword = new WText(comXPassword, SWT.BORDER, "", "不修改密码则不需要修改");
		txtXPassword.setTextLimit(32);
		FormData fd_txtXPassword = new FormData();
		fd_txtXPassword.left = new FormAttachment(lblXPassword, 4);
		fd_txtXPassword.top = new FormAttachment(comXPassword, 2);
		fd_txtXPassword.right = new FormAttachment(100, -8);
		fd_txtXPassword.bottom = new FormAttachment(txtXPassword, 30);
		txtXPassword.setLayoutData(fd_txtXPassword);
		
		// IP
		WComposite comXIP = new WComposite(mainCom, SWT.NONE);
		comXIP.setLayout(new FormLayout());
		FormData fd_comXIP = new FormData();
		fd_comXIP.left = new FormAttachment(mainCom, 4);
		fd_comXIP.top = new FormAttachment(comSID, 4);
		fd_comXIP.right = new FormAttachment(25, 0);
		fd_comXIP.bottom = new FormAttachment(comXIP, 35);
		comXIP.setLayoutData(fd_comXIP);
		WLabel lblXIP = new WLabel(comXIP, SWT.BORDER, Const.FONT16, "IP", SWT.RIGHT);
		FormData fd_lblXIP = new FormData();
		fd_lblXIP.left = new FormAttachment(comXIP, 0);
		fd_lblXIP.top = new FormAttachment(comXIP, 2);
		fd_lblXIP.right = new FormAttachment(30);
		fd_lblXIP.bottom = new FormAttachment(lblXIP, 30);
		lblXIP.setLayoutData(fd_lblXIP);
		txtXIP = new WText(comXIP, SWT.BORDER, "", "IP");
		txtXIP.setTextLimit(32);
		FormData fd_txtXIP = new FormData();
		fd_txtXIP.left = new FormAttachment(lblXIP, 4);
		fd_txtXIP.top = new FormAttachment(comXIP, 2);
		fd_txtXIP.right = new FormAttachment(100, -8);
		fd_txtXIP.bottom = new FormAttachment(txtXIP, 30);
		txtXIP.setLayoutData(fd_txtXIP);
		
		// 计算机
		WComposite comXPCName = new WComposite(mainCom, SWT.NONE);
		comXPCName.setLayout(new FormLayout());
		FormData fd_comXPCName = new FormData();
		fd_comXPCName.left = new FormAttachment(comXIP, 4);
		fd_comXPCName.top = new FormAttachment(comXName, 4);
		fd_comXPCName.right = new FormAttachment(50, 0);
		fd_comXPCName.bottom = new FormAttachment(comXPCName, 35);
		comXPCName.setLayoutData(fd_comXPCName);
		WLabel lblXPCName = new WLabel(comXPCName, SWT.BORDER, Const.FONT16, "计算机", SWT.RIGHT);
		FormData fd_lblXPCName = new FormData();
		fd_lblXPCName.left = new FormAttachment(comXPCName, 0);
		fd_lblXPCName.top = new FormAttachment(comXPCName, 2);
		fd_lblXPCName.right = new FormAttachment(30);
		fd_lblXPCName.bottom = new FormAttachment(lblXPCName, 30);
		lblXPCName.setLayoutData(fd_lblXPCName);
		txtXPCName = new WText(comXPCName, SWT.BORDER, "", "计算机");
		txtXPCName.setTextLimit(32);
		FormData fd_txtXPCName = new FormData();
		fd_txtXPCName.left = new FormAttachment(lblXPCName, 4);
		fd_txtXPCName.top = new FormAttachment(comXPCName, 2);
		fd_txtXPCName.right = new FormAttachment(100, -8);
		fd_txtXPCName.bottom = new FormAttachment(txtXPCName, 30);
		txtXPCName.setLayoutData(fd_txtXPCName);

		// 备注
		WComposite comXNote = new WComposite(mainCom, SWT.NONE);
		comXNote.setLayout(new FormLayout());
		FormData fd_comXNote = new FormData();
		fd_comXNote.left = new FormAttachment(comXPCName, 4);
		fd_comXNote.top = new FormAttachment(comXAccount, 4);
		fd_comXNote.right = new FormAttachment(100, -8);
		fd_comXNote.bottom = new FormAttachment(comXNote, 35);
		comXNote.setLayoutData(fd_comXNote);
		
		WLabel lblXNote = new WLabel(comXNote, SWT.BORDER, Const.FONT16, "备注", SWT.RIGHT);
		FormData fd_lblXNote = new FormData();
		fd_lblXNote.left = new FormAttachment(comXNote, 0);
		fd_lblXNote.top = new FormAttachment(comXNote, 2);
		fd_lblXNote.right = new FormAttachment(15);
		fd_lblXNote.bottom = new FormAttachment(lblXNote, 30);
		lblXNote.setLayoutData(fd_lblXNote);
		txtXNote = new WText(comXNote, SWT.BORDER, "", "备注");
		txtXNote.setTextLimit(32);
		FormData fd_txtXNote = new FormData();
		fd_txtXNote.left = new FormAttachment(lblXNote, 4);
		fd_txtXNote.top = new FormAttachment(comXNote, 2);
		fd_txtXNote.right = new FormAttachment(100, -8);
		fd_txtXNote.bottom = new FormAttachment(txtXNote, 30);
		txtXNote.setLayoutData(fd_txtXNote);
		
		self.pack();
		ShellUtil.center(self);
		//this.texts.add(txtSID);
		this.texts.add(txtXName);
		this.texts.add(txtXAccount);
		this.texts.add(txtXPassword);
		this.texts.add(txtXIP);
		this.texts.add(txtXPCName);
		this.texts.add(txtXNote);
		show();
	}
	

	@Override
	protected String title() {
		return "用户编辑";
	}

	@Override
	protected boolean checkModelChange() {
		boolean rs = false;
		if(model != null) {
			rs = !model.getStr("sID").equals(txtSID.getText()) 
					|| !model.getStr("xName").equals(txtXName.getText())
					|| !txtXPassword.getText().equals("") 
					|| !model.getStr("xAccount").equals(txtXAccount.getText())
					|| !model.getStr("xIP").equals(txtXIP.getText()) 
					|| !model.getStr("xPCName").equals(txtXPCName.getText())
					|| !model.getStr("xNote").equals(txtXNote.getText());
		}else {
			rs = !txtXName.getText().equals("") && !txtXAccount.getText().equals("");
		}
		return rs;
	}

	
	// 保存这个不好搞
	@Override
	protected boolean save() {
		if(checkInput()) {
			if(sID <= 0 || model == null) {
				model = new YTUser();
				model.setSID(Integer.valueOf(txtSID.getText()))
				.set("xOption", 0)
				.setRAuthorID(dda.user.getSID())
				.setROwnerID(dda.user.getSID())
				.setSStatus(2);
			}
			model.set("xName", txtXName.getText())
			.set("xAccount", txtXAccount.getText())
			.set("xIP", txtXIP.getText())
			.set("xPCName", txtXPCName.getText())
			.set("xNote", txtXNote.getText())
			;
			if(!txtXPassword.getText().equals("")) {
				model.set("xPassword", txtXPassword.getText());
			}
			if(sID > 0) {
				return masterService.update(model);
			}else {
				return masterService.save(model);
			}
				
		}
		return false;
	}
	
	private boolean checkInput() {
		boolean rs = true;
		String msg = "";
		// sID xName xAccount xPassword xIP xPCName xNote
		if(sID > 0) {	// 如果是编辑
			if(rs) {
				String sid = txtSID.getText();
				if(sid.length() == 0 || sID != Integer.valueOf(sid)) {
					rs = false;
					msg = "sID异常";
				}
			}
		}
		if(sID <= 0) {
			if(rs) {
				if(txtXPassword.getText().equals("")) {
					rs = false;
					msg = "密码不能为空";
				}
			}
		}
		if(rs) {
			if(txtXName.getText().equals("")) {
				rs = false;
				msg = "名称不能为空";
			}
		}
		if(rs) {
			if(txtXAccount.getText().equals("")) {
				rs = false;
				msg = "账号不能为空";
			}
		}
		if(!rs) {
			Msg.error(self, msg);
		}
		return rs;
	}
	
	
	@Override
	protected void show() {
		txtSID.removeModifyListener(modifyListener);
		txtXName.removeModifyListener(modifyListener);
		txtXAccount.removeModifyListener(modifyListener);
		txtXPassword.removeModifyListener(modifyListener);
		txtXIP.removeModifyListener(modifyListener);
		txtXPCName.removeModifyListener(modifyListener);
		txtXNote.removeModifyListener(modifyListener);
		if(model != null) {
			txtSID.setText(model.getStr("sID"));
			txtXName.setText(model.getStr("xName"));
			txtXAccount.setText(model.getStr("xAccount"));
			txtXPassword.setText("");
			txtXIP.setText(model.getStr("xIP"));
			txtXPCName.setText(model.getStr("xPCName"));
			txtXNote.setText(model.getStr("xNote"));
		}else {
			txtSID.setText(String.valueOf(DDA.getInstance().getIDService.exec(0, "yt_user", 1)));
			txtXName.setText("");
			txtXAccount.setText("");
			txtXPassword.setText("");
			txtXIP.setText("");
			txtXPCName.setText("");
			txtXNote.setText("");
		}
		txtSID.addModifyListener(modifyListener);
		txtXName.addModifyListener(modifyListener);
		txtXAccount.addModifyListener(modifyListener);
		txtXPassword.addModifyListener(modifyListener);
		txtXIP.addModifyListener(modifyListener);
		txtXPCName.addModifyListener(modifyListener);
		txtXNote.addModifyListener(modifyListener);
	}


	
	public static void main(String[] args) {
		new UserEditShell2().go();
	}
}
